import java.io.*;
import java.net.*;
import java.util.*;
import java.security.Security;
import java.security.Provider;
import javax.net.ssl.*;
import local.util.*;
import java.text.DecimalFormat;

public class RealDataCollecter {    
    private boolean init      = true; 
    private boolean debugOn   = false;
    private String tickName   = null;
    private String urlAddress = null;
    private String sInput     = null;
    private Vector vInput     = null;
    private Vector vOutHead   = null;
    private Vector vOutBody   = null;
    private String  lastPrice  = "0";
    private String  lastChange = "0";
    private String  lastVolume = "0";
    private int    vUp         = 0;
    private int    vDown       = 0;
    private int    vUnchange   = 0;
    private String inQueryString   = null;

    String init_price   = "0.0";
    String temp_price   = "0.0";
    String temp_price_up   = "0.0";
    String temp_price_down   = "0.0";
    String init_time    = "";
    String temp_time    = "";
    String temp_time_up    = "";
    String temp_time_down    = "";
    String init_volume  = "0";
    String temp_volume  = "0";
    int total_volume_down  = 0;
    int total_volume_up  = 0;
    String temp_volume_up  = "0";
    String temp_volume_down  = "0";


    private static Hashtable dataHashTable = new Hashtable();
    private static Hashtable tickHashTable = new Hashtable();
    private static DecimalFormat twoDigits = new DecimalFormat("0.00");

    public static void main (String args[]) {

	if (args.length != 2) {
	    MyPrint.printLine("============================================", 1);
	    MyPrint.message("[1][TICK][Test Using DB data]");
	    MyPrint.message("[2][TICK][Test Using URL data]");
	    MyPrint.printLine("============================================", 1);
	    System.exit(0);
	}

	RealDataCollecter myData = new RealDataCollecter();
	myData.setTick(args[1]);
	if (args[0].equals("1")) {
	    String out_query_string ="SELECT * FROM detail_test_record";
	    DataBaseQuery my_query = new DataBaseQuery ();
	    my_query.setQueryString (out_query_string);
	    my_query.lookup (out_query_string);
	    myData.vOutHead=my_query.getTableHeadVector();
	    myData.vOutBody=my_query.getTableRecordVector();

	    Enumeration enum = myData.vOutBody.elements();
	    while (enum.hasMoreElements()) {
		//System.out.println(enum.nextElement());
		myData.setReturnHashTable(myData.vOutHead, (Vector)enum.nextElement());
		myData.loadTestData();
	    }
	}
	else if (args[0].equals("2")) {
	    for (int i =0; i <20; i++){
		String try_url     = "";

		try_url = "https://www.bigcharts.com/custom/datek-com/datek-rt2.asp?osymb=&symb="+myData.getTick()+
		    "&time=1dy&freq=1mi&compidx=aaaaa%7E0&comp=&type=2&ma=0&maval=9&uf=0&lf=1&"+
		    "lf2=0&lf3=0&sid=&x=42&y=6";
		myData.setUrl(try_url);
		myData.loadData();
		//myData.insertIntoDataBase();
	    }
	}
    }

    public void loadData () {
	sInput     = "";
	vInput     = new Vector(10);
	vOutHead   = new Vector(10);
	vOutBody   = new Vector(10);

	/**
	 * connect to url which will get return string [sInput]
	 */
	int temp1 = 0;
	int temp2 = 0;

	String this_price   = "0.0";
	String this_volume  = "0";
	String this_change  = "0.0";
	String this_high    = "0.0";
	String this_low     = "0.0";
	String prev_close   = "0.0";
	String sign         ="";

	connectToUrl(getUrl());
	setReturnVector (getReturnString());

	setHeadAndBodyReturnVector (getReturnVector());
	setReturnHashTable(getReturnHeadVector(), getReturnBodyVector());

	if (getReturnHashtable() == null) {
	    return;
	}

	this_price   = ((String)(getReturnHashtable().get("Last"  ))).replaceAll("n/a","");
	//this_change  = ((String)(getReturnHashtable().get("Change"))).replaceAll("n/a","");
	this_volume  = ((String)(getReturnHashtable().get("Volume"))).replaceAll(",","");
	//this_high    = ((String)(getReturnHashtable().get("High"  ))).replaceAll("n/a","");
	//this_low     = ((String)(getReturnHashtable().get("Low"   ))).replaceAll("n/a","");

	if (this_volume.equals(lastVolume)) {
	    return;
	}

	if (init) {
	    init_time = MyDateAndTime.getCurrentTime(24);
	    init_price = this_price;
	    init_volume = this_volume;
	    temp_time_up = MyDateAndTime.getCurrentTime(24);
	    temp_price_up = this_price;
	    temp_volume_up = this_volume;
	    temp_time_down = MyDateAndTime.getCurrentTime(24);
	    temp_price_down = this_price;
	    temp_volume_down = this_volume;
	 
	    System.out.println("========================================================================================");
	    System.out.print(init_time+"\t");
	    System.out.print("Volume:"+(init_volume)+"\t");
	    System.out.println("Price:"+(init_price)+"\t");
	    System.out.println("========================================================================================");
	    
	    init = false;
	}
     
	if ((Float.valueOf(this_price)).floatValue() > (Float.valueOf(temp_price_up)).floatValue()) {
	    temp_time_up = MyDateAndTime.getCurrentTime(24);
	    temp_price_up = this_price;
	    temp_volume_up = this_volume;
	}

	if ((Float.valueOf(this_price)).floatValue() < (Float.valueOf(temp_price_down)).floatValue()) {
	    temp_time_down = MyDateAndTime.getCurrentTime(24);
	    temp_price_down = this_price;
	    temp_volume_down = this_volume;
	}

	//System.out.println("This Price:"+this_price+"\t");

	if ((Float.valueOf(this_price)).floatValue()<(Float.valueOf(temp_price_up)).floatValue() &&  
	    (Float.valueOf(this_price)).floatValue()>(Float.valueOf(temp_price_down)).floatValue()) {
	    if ((Float.valueOf(temp_price_up)).floatValue()>(Float.valueOf(init_price)).floatValue()) {  
		System.out.print("From:"+init_time+"\t");
		System.out.print("To:"+temp_time_up+"\t");
		System.out.print(this_price+"\t");
		System.out.print("Up\t");
		total_volume_up = total_volume_up +(Integer.parseInt(temp_volume_up)-Integer.parseInt(init_volume));
		System.out.print("Volume:"+(Integer.parseInt(temp_volume_up)-Integer.parseInt(init_volume))+"\t");
		System.out.print("Change:"+((Float.valueOf(temp_price_up)).floatValue()-(Float.valueOf(init_price)).floatValue())+"\t");
		init_time = temp_time_up;
		init_price = temp_price_up;
		init_volume = temp_volume_up;
		temp_time_down = temp_time_up;
		temp_price_down = temp_price_up;
	  
		temp_volume_down = temp_volume_up;
		System.out.print("Total Up:"+total_volume_up+"\t");
		System.out.println("Total Down:"+total_volume_down+"\t");
	    }
	    else if ((Float.valueOf(temp_price_down)).floatValue()<(Float.valueOf(init_price)).floatValue()) {  
		System.out.print("From:"+init_time+"\t");
		System.out.print("To:"+temp_time_down+"\t");
		System.out.print(this_price+"\t");
		System.out.print("Down\t");
		total_volume_down = total_volume_down+(Integer.parseInt(temp_volume_down)-Integer.parseInt(init_volume));
		System.out.print("Volume:"+(Integer.parseInt(temp_volume_down)-Integer.parseInt(init_volume))+"\t");
		System.out.print("Change:"+((Float.valueOf(temp_price_down)).floatValue()-(Float.valueOf(init_price)).floatValue())+"\t");
		init_time = temp_time_down;
		init_price = temp_price_down;
		init_volume = temp_volume_down;
		temp_time_up = temp_time_down;
		temp_price_up = temp_price_down;
		//total_volume_down +=total_volume_down;
		temp_volume_up = temp_volume_down;
		System.out.print("Total Up:"+total_volume_up+"\t");
		System.out.println("Total Down:"+total_volume_down);
	    }
	} 
	/*
	inQueryString = "INSERT INTO detail_record VALUES ('"+
	    MyDateAndTime.getCurrentDate()+"','"+getTick()+"',"+ 
	    this_price+","+this_change+","+this_high+","+this_low+", '"+this_volume+"',"+ 
	    vUp+","+ vDown+","+vUnchange+","+prev_close+");";
    
	if ((MyDateAndTime.getCurrentTime(24)).startsWith("16:1") &&(this_price.equals(lastPrice))) {
	    insertIntoDataBase();
	}
	*/
	lastPrice = (String)(getReturnHashtable()).get("Last");
	lastVolume = ((String)dataHashTable.get("Volume")).replaceAll(",","");
	//System.out.println();
    }

    public void loadTestData () {

	int temp1 = 0;
	int temp2 = 0;
	String this_time   = "0:0";
	String this_price   = "0.0";
	String this_volume  = "0";
	String this_change  = "0.0";
	String this_high    = "0.0";
	String this_low     = "0.0";
	String prev_close   = "0.0";
	String sign         ="";

	//setTestReturnHashTable(vOutHead, vOutBody);

	this_price   = ((String)(getReturnHashtable().get("Last"  ))).replaceAll("n/a","");
	this_change  = ((String)(getReturnHashtable().get("Change"))).replaceAll("n/a","");
	this_volume  = ((String)(getReturnHashtable().get("Volume"))).replaceAll(",","");
	this_high    = ((String)(getReturnHashtable().get("High"  ))).replaceAll("n/a","");
	this_low     = ((String)(getReturnHashtable().get("Low"   ))).replaceAll("n/a","");
	this_time    = (String)(getReturnHashtable().get("Time"   ));

     if (this_volume.equals(lastVolume)) {
	 System.out.println("-----------------not change-----------------");
	 return;
     }
     // System.out.println("Last:"+this_price);
     if (init) {
	 init_time = this_time;
	 init_price = this_price;
	 init_volume = this_volume;
	 temp_time_up = this_time;
	 temp_price_up = this_price;
	 temp_volume_up = this_volume;
	 temp_time_down = this_time;
	 temp_price_down = this_price;
	 temp_volume_down = this_volume;
	 /*
	 System.out.println("========================================================================================");
	 System.out.println(init_time+"\t");
	 System.out.println("Volume:"+(init_volume)+"\t");
	 System.out.println("Price:"+(init_price)+"\t");
	 System.out.println("========================================================================================");
	 */
	 init = false;
     }
   
     if ((Float.valueOf(this_price)).floatValue() > (Float.valueOf(temp_price_up)).floatValue()) {
	 temp_time_up = this_time;
	 temp_price_up = this_price;
	 temp_volume_up = this_volume;
	 //System.out.println("set up Time:"+(temp_time_up)+"\t");
	 //System.out.println("set Up Volume:"+(temp_volume_up)+"\t");
	 //System.out.println("set Up Price:"+(temp_price_up)+"\t");
     }

     //System.out.println("temp_price_up"+temp_price_up);

     if ((Float.valueOf(this_price)).floatValue() < (Float.valueOf(temp_price_down)).floatValue()) {
	 temp_time_down = this_time;
	 temp_price_down = this_price;
	 temp_volume_down = this_volume;
	 //System.out.println("set Time:"+(temp_time_down)+"\t");
	 //System.out.println("set Down Volume:"+(temp_volume_down)+"\t");
	 //System.out.println("set Down Price:"+(temp_price_down)+"\t");
     }

     if ((Float.valueOf(this_price)).floatValue()<(Float.valueOf(temp_price_up)).floatValue() &&  
	 (Float.valueOf(this_price)).floatValue()>(Float.valueOf(temp_price_down)).floatValue()) {
	 if ((Float.valueOf(temp_price_up)).floatValue()>(Float.valueOf(init_price)).floatValue()) {  
	     System.out.print("From:"+init_time+"\t");
	     System.out.print("To:"+temp_time_up+"\t");
	     System.out.print("Up\t");
	     System.out.print("Volume:"+(Integer.parseInt(temp_volume_up)-Integer.parseInt(init_volume))+"\t");
	     System.out.println("Change:"+((Float.valueOf(temp_price_up)).floatValue()-(Float.valueOf(init_price)).floatValue())+"\t");
	     init_time = temp_time_up;
	     init_price = temp_price_up;
	     init_volume = temp_volume_up;
	     temp_time_down = temp_time_up;
	     temp_price_down = temp_price_up;
	     temp_volume_down = temp_volume_up;
	 }
	 else if ((Float.valueOf(temp_price_down)).floatValue()<(Float.valueOf(init_price)).floatValue()) {  
	     System.out.print("From:"+init_time+"\t");
	     System.out.print("To:"+temp_time_down+"\t");
	     System.out.print("Down\t");
	     System.out.print("Volume:"+(Integer.parseInt(temp_volume_down)-Integer.parseInt(init_volume))+"\t");
	     System.out.println("Change:"+((Float.valueOf(temp_price_down)).floatValue()-(Float.valueOf(init_price)).floatValue())+"\t");
	     init_time = temp_time_down;
	     init_price = temp_price_down;
	     init_volume = temp_volume_down;
	     temp_time_up = temp_time_down;
	     temp_price_up = temp_price_down;
	     temp_volume_up = temp_volume_down;
	 }
     } 
   
     inQueryString = "INSERT INTO detail_record VALUES ('"+
	             MyDateAndTime.getCurrentDate()+"','"+getTick()+"',"+ 
	             this_price+","+this_change+","+this_high+","+this_low+", '"+this_volume+"',"+ 
	             vUp+","+ vDown+","+vUnchange+","+prev_close+");";
     /*
     if ((MyDateAndTime.getCurrentTime(24)).startsWith("16:1") &&(this_price.equals(lastPrice)) &&(this_volume.equals(lastVolume))) {
	 insertIntoDataBase();
     }
     */
     lastPrice = (String)(getReturnHashtable()).get("Last");
     lastVolume = ((String)dataHashTable.get("Volume")).replaceAll(",","");
    }

    public void setTick (String tick) {
	tickName = tick;
    }

    public String getTick () {
	return tickName;
    }

    public void setUrl (String url) {
	urlAddress = url;
    }

    public String getUrl () {
	return urlAddress;
    }

    public String getReturnString () {
	return sInput;
    }

    private void connectToUrl (String url_address) {
        URL url = null;        
	HttpsURLConnection httpsConnection = null;
	String up;

	try {            
	    url = new URL (url_address);  
	    try {                
		httpsConnection = (HttpsURLConnection)url.openConnection();                
		httpsConnection.setDoOutput(false);                
		httpsConnection.connect();
                
		if(debugOn) {
		    System.out.println("Connection Code and Message:" + 
				   httpsConnection.getResponseCode() + 
				   " - " + httpsConnection.getResponseMessage());
		}

		BufferedReader in = new BufferedReader(new InputStreamReader
		    (httpsConnection.getInputStream()));

		StringBuffer buff1= new StringBuffer("");

		sInput = in.readLine();

		while (sInput != null) {
		    buff1.append(sInput);
		    sInput = in.readLine();
		}

		in.close();            

		sInput =  buff1.toString();
		sInput=sInput.replaceAll("\n","");
		sInput=sInput.replaceAll("\t","");
		sInput=sInput.replaceAll("&nbsp;","");
		sInput=sInput.replaceAll(">",">\n");
		sInput=sInput.replaceAll("</","\n</");

		if(debugOn) {
		    System.out.println("End of loading data from url"+" data string length="+sInput.length());
		}
	    } catch (IOException ioe) {                
		System.out.println(ioe.getMessage());  
		insertIntoDataBase();
	    }        
	} catch (MalformedURLException mue) {            
	     System.out.println(mue.getMessage());
	     insertIntoDataBase();
	} catch (Exception e) {            
	    e.printStackTrace();            
	    insertIntoDataBase();
	}    
    }

    public void setReturnVector (String s_input) {

	if(debugOn) {
	    System.out.println("Putting Data String into Vector, legth="+s_input.length());
	}

	String element = "";
	StringTokenizer tokens = new StringTokenizer(s_input, "\n");
	
	while (tokens.hasMoreTokens()) {
	    element = tokens.nextToken();
	    if (element != null) {
		vInput.addElement(element);
	    }
	}

	if(debugOn) {
	    System.out.println("Data inserted into vector size="+vInput.size());
	}
    }

    public Vector getReturnVector() {
	return vInput;
    }

    private void setHeadAndBodyReturnVector (Vector v_in) {

	if(debugOn) {
	    System.out.println("Before select useful info, vector size is:"+v_in.size());
	}

	String text = "";
	Enumeration penum = v_in.elements();
       
	/**
	 * This while loop will remove all the over head
	 **/

	while ( penum.hasMoreElements()) {

	    text = (String)penum.nextElement();

	    if (text.startsWith("<font face=\"Arial,Helvetica,Geneva\" size=\"1\">")) {
		break;
	    }
	}
	
	/**
	 * Start the data we needed
	 **/

	while ( penum.hasMoreElements() ) {
	    text = (String)penum.nextElement();

	    if (text.startsWith("HELP")) {
		break;
	    }

	    if (!(text.startsWith("<") || text.startsWith("+") || text.startsWith("to<"))) {
	
		if (text.equals("Last")) {
		    vOutHead.addElement(text);
		}
		else if (text.equals("Change")) {
		    vOutHead.addElement(text);
		}
		else if (text.equals("High")) {
		    vOutHead.addElement(text);
		}
		else if (text.equals("Low")) {
		    vOutHead.addElement(text);
		}
		else if (text.equals("Volume")) {
		    vOutHead.addElement(text);
		}
		else if (text.equals("Prev. Close")) {
		    vOutHead.addElement(text);
		}
		else if (text.equals("Day's Range")) {
			vOutHead.addElement("Day LOW");
			vOutHead.addElement("Day HIGH");
		}
		else if (text.startsWith("52 Week")) {
			vOutHead.addElement("52_LOW");
			vOutHead.addElement("52_HIGH");
		}
		else if (MyStringCheck.isNumber(text)) {
		    vOutBody.addElement(text);
		}
		else {
		    //System.out.println(text +" not added");
		}
	    }
	}
    }

    public Vector getReturnHeadVector () {
	return vOutHead;
    }

    public Vector getReturnBodyVector () {
	return vOutBody;
    }

    public void printData(Hashtable dataTable) {
	String temp; int i = 0;
	String temp2="";

	System.out.print(dataTable.get("Last")+"\t");  
	System.out.print(dataTable.get("Change")+"\t");  
	System.out.print(dataTable.get("High")+"\t");  
	System.out.print(dataTable.get("Low")+"\t");
	temp = (String)dataTable.get("Volume");
	temp = temp.replaceAll(",","");
	i = 10 - temp.length();

	if (i>0) {
	    for (int m=0; m<i; m++) {
		temp2 = temp2 +" ";
	    }
	}

	System.out.print(temp2+temp+"\t\t");
	System.out.println(dataTable.get("Prev. Close")+"\t");
    } 


    public void setReturnHashTable(Vector v_head, Vector v_body ) {

	Object key[]    = v_head.toArray();
	Object value[]  = v_body.toArray();

	if (v_head.size() != v_body.size()) {
	    dataHashTable = null;
	    return;
	}

	for(int i=0; i<value.length; i++) {
	    // System.out.println("key["+i+"]="+key[i]+";valume="+value[i]);
	    dataHashTable.put(key[i], value[i]);
	}
    }

    public void setTestReturnHashTable(Vector v_head, Vector v_body ) {

	Object key[]    = v_head.toArray();
	Object value[]  = v_body.toArray();

	for(int i=0; i<key.length; i++) {
	    // System.out.println("key["+i+"]="+key[i]+";valume="+value[i]);
	    dataHashTable.put(key[i], value[i]);
	}
    }


    public Hashtable getReturnHashtable() {
	return dataHashTable;
    }

    public void insertIntoDataBase() {
	//System.out.println(inQueryString);
	DataBaseInsert myInsert = new DataBaseInsert();	
	myInsert.setQueryString(inQueryString);
	myInsert.insertData();
	System.exit(0);
    }   
}






